#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

class exce
{
protected:
    char text[512];

public:
    exce(const char *ptext)
    {
        strcpy(text, ptext);
    }
    const char *what()
    {
        return text;
    }
};

class openFileExec : public exce
{
private:
    char filename[128];
    int position;

public:
    openFileExec(const char *ptext, const char *filename, int position) : exce(ptext)
    { // 因为实在父类的基础上做补充，所以需要将子类收集到的信息传递给父类
        strcpy(this->filename, filename);
        this->position = position;
    }
    char *getName()
    {
        return filename;
    }
    int getPos()
    {
        return position;
    }
};


float divide(int x, int y)
{
    if (y == 0)
    {
        throw new exce("被除数不能为0");
        return 0;
    }
    return static_cast<float>(x) / y;
};

void openFile()
{
    FILE *p = fopen("1.txt", "r");
    if (p == NULL)
    {
        throw new openFileExec("文件打开失败", "1.txt", __LINE__);
    }
    cout << "文件打开成功" << endl;
}

int main(int argc, char const *argv[])
{
    /*
    int i, j;
    cout << " 请输入除数和被除数" << endl;
    cin >> i >> j;
    */
    try
    {
        openFile();
    }
    catch (openFileExec *p)
    {
        cout << p->what() << "  " << p->getName() << "  " << p->getPos() << endl;
    }
    catch (exce *p)
    {
        cout << p->what() << endl;
    }
    return 0;
}
